package com.bwsoft.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.bwsoft.domain.Fun;
import com.bwsoft.domain.User;
import com.bwsoft.domain.UserGroup;
import com.bwsoft.model.Message;
import com.bwsoft.model.ZTreeV;
import com.bwsoft.service.UserGroupService;

@SuppressWarnings("rawtypes")
@Service(UserGroupService.NAME)
public class UserGroupServiceImpl extends BaseServiceImpl implements UserGroupService {

	private static Logger LOG = Logger.getLogger(UserGroupServiceImpl.class.getClass());

	/**
	 * 获取整个的UserGroupList集合，并且将每个userGroup下面的User集合的名字拼接
	 */
	@Override
	public List<UserGroup> findAllUserGroup() {

		List<UserGroup> userGroupList = userGroupDao.findAll();
		/* 将每个userGroup下面的User集合的名字拼接 */
		for (UserGroup userGroup : userGroupList) {
			StringBuffer sb = new StringBuffer();
			for (User user : userGroup.getUsers()) {
				sb.append(sb.toString() + "," + user.getName());
			}
			if (sb.length() > 0) {
				userGroup.setUserNames(sb.substring(1, sb.length()));
			}
		}

		return userGroupList;
	}

	/**
	 * 添加用户组
	 */
	@Override
	public Message saveUser(UserGroup userGroup) {
		try {
			userGroupDao.save(userGroup);
			message.setMsgFlag(true);
			message.setMsgStr("操作成功！");
		} catch (Exception e) {
			LOG.info("添加用户组失败" + e);
			message.setMsgFlag(false);
			message.setMsgStr("操作失败！");
		}
		return message;
	}

	/**
	 * 删除用户组
	 */
	@Override
	public Message deleteUser(UserGroup userGroup) {

		if (userGroup.getUserNames().isEmpty() || userGroup.getUserNames() == "") {
			message.setMsgFlag(false);
			message.setMsgStr("请勾选需要删除的节点！");
			return message;
		}

		try {

			userGroupDao.executeByHql("delete from UserGroup ug where ug in (" + userGroup.getUserNames() + ")");
			message.setMsgFlag(true);
			message.setMsgStr("操作成功！");
		} catch (Exception e) {
			LOG.info("添加用户组失败" + e);
			message.setMsgFlag(false);
			message.setMsgStr("操作失败！");
		}
		return message;
	}

	/**
	 * 查询要修改的详情
	 */
	@Override
	public UserGroup getUserGroupInfo(UserGroup userGroup) {

		return userGroupDao.findById(userGroup.getGroupID());
	}

	/**
	 * 修改
	 */
	@Override
	public Message updateUserGroup(UserGroup userGroup) {
		if (userGroup.getGroupID() == null) {
			message.setMsgFlag(false);
			message.setMsgStr("请选择需要修改的用户组！");
			return message;
		}
		if (userGroup.getName().isEmpty() || userGroup.getName() == "") {
			message.setMsgFlag(false);
			message.setMsgStr("用户组名不能为空！");
			return message;
		}

		UserGroup ug = userGroupDao.findById(userGroup.getGroupID());
		ug.setDescription(userGroup.getDescription());
		ug.setName(userGroup.getName());

		try {
			userGroupDao.saveOrUpdate(ug);
			message.setMsgFlag(true);
			message.setMsgStr("操作成功！");
		} catch (Exception e) {
			message.setMsgFlag(false);
			message.setMsgStr("修改用户组异常！");
			LOG.info("修改用户组异常" + e);
		}
		return message;
	}

	@Override
	public List getZtreeList(UserGroup userGroup) {
		userGroup = userGroupDao.findById(userGroup.getGroupID());
		List<ZTreeV> ztreeList = new ArrayList<ZTreeV>();
		// 获取所有功能List
		List<Fun> funList = funDao.findAll();
		// 获取该用户组已有权限的List
		Set<Fun> userGroupFuns = userGroup.getFuns();

		for (Fun fun : funList) {
			ZTreeV z = new ZTreeV();
			z.id = fun.getFunID() + "";
			z.pId = fun.getParentFunID() + "";
			z.name = fun.getFunName();
			z.open = fun.getParentFunID() == 0;
			z.chkDisabled = false;
			z.Checked = false;
			// 判断用户是否有这个功能
			for (Fun fun2 : userGroupFuns) {
				if (fun2.getFunID().equals(fun.getFunID())) {
					z.Checked = true;
					break;
				}
			}
			ztreeList.add(z);
		}
		return ztreeList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Message saveTree(UserGroup userGroup) {

		try {
			UserGroup ug = userGroupDao.findById(userGroup.getGroupID());

			ug.setFuns(new HashSet<Fun>());

			if (!userGroup.getUserNames().isEmpty() && userGroup.getUserNames() != "") {
				List<Fun> funList = funDao.findByHql("from Fun a where a.funID in (" + userGroup.getUserNames() + ")");
				for (Fun fun : funList) {
					ug.getFuns().add(fun);
				}
			}

			userGroupDao.saveOrUpdate(userGroupDao.getHibernateTemplate().merge(ug));
			message.setMsgFlag(true);
			message.setMsgStr("操作成功！");
		} catch (Exception ex) {
			ex.printStackTrace();
			LOG.info("保存用户权限树异常");
			throw new RuntimeException();

		}

		return message;
	}
}
